package com.bjpowernode.controller;

import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.entity.Student;
import com.bjpowernode.utils.MybatisUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

@WebServlet("/SelectStudentServlet")
public class SelectStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        StudentDao dao= sqlSession.getMapper(StudentDao.class);
        Integer pageNum=Integer.valueOf(request.getParameter("page"));
        PageHelper.startPage(pageNum,3);
        List<Student> students=dao.pageSelectStudents();
        ObjectMapper om=new ObjectMapper();
        String studentJson=om.writeValueAsString(students);
        response.setContentType("text/html;charset=utf-8");
        Writer out=response.getWriter();
        System.out.println(studentJson);
        out.write(studentJson);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
